package com.ficus.road.maintain.process;

import com.ficus.road.maintain.core.model.Confirmation;
import com.ficus.road.maintain.core.model.DelayFrameDTO;
import com.ficus.road.maintain.core.model.FrameDTO;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.service.HitService;
import com.ficus.road.maintain.websocket.WebSocketManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.DelayQueue;

@Slf4j
public class DrawerProcessor extends Thread {

    WebSocketManager webSocketManager;
    DelayQueue<DelayFrameDTO> blockingDeque = new DelayQueue<>();
    HitService hitService;
    int delayTime;

    public DrawerProcessor(String name, WebSocketManager webSocketManager, HitService hitService, int delayTime) {
        super(name);
        this.webSocketManager = webSocketManager;
        this.delayTime = delayTime;
        this.hitService = hitService;
    }

    @Override
    public void run() {
        while (true) {
            try {
                //通过两个子类获取FrameDTO
                //DrawerProcessor用的是延迟队列，ReplayDrawerProcessor用的是自己实现的队列
                DelayFrameDTO value = getItem();
                //处理FrameDTO
                onProcess(value.getFrameDTO());
            } catch (Exception e) {
                log.error("Base message processor error", e);
            }
        }
    }

    public void put(FrameDTO value) {
        if (null == value) {
            return;
        }
        DelayFrameDTO delayFrameDTO = DelayFrameDTO.fromFrameDTO(value, delayTime);
        blockingDeque.put(delayFrameDTO);

    }

    public DelayFrameDTO getItem() throws InterruptedException {
        return blockingDeque.take();
    }

    public void onProcess(FrameDTO value) {
        //找到这条历史告警，看是否审核通过，这里需要把状态加过来
        String diseaseUid = null;
        //大部分数据帧里并没有病害，但是每一帧都有diseaseUid
        //找出有病害的帧
        boolean checked = false;
        if (CollectionUtils.isNotEmpty(value.getHits())) {
            for (Hit hit : value.getHits()) {
                diseaseUid = hit.getAttrs().getDiseaseUid();
                if (StringUtils.isNotBlank(diseaseUid)) {
                    Criteria criteria = Criteria.where("list.attrs.disease_uid").is(diseaseUid).and("attrs.confirmation.status").is(Confirmation.ConfirmationStatus.CORRECT.toString());
                    List<Hit> hits = hitService.queryHits(new Query(criteria));
                    if (CollectionUtils.isNotEmpty(hits)) {
                        checked = true;
                        hit.getAttrs().getConfirmation().setStatus(Confirmation.ConfirmationStatus.CORRECT);
                        break;
                    }
                }
            }
        }

        Long timeStamp = value.getTimeStamp();

        // 如果告警审核没通过，roadDiseaseResult就设为空
        if (!checked) {
            value.setRoadDiseaseResult(new ArrayList<>());
            value.setHits(new ArrayList<>());
        }
        value.setTimeStamp(timeStamp / 1000);
        value.setSendTimeStamp(value.getSendTimeStamp() / 1000);
        //视频回放的时候传入uuid
        webSocketManager.sentMessageById(value, value.getUuid());
    }

}
